﻿@{
    Layout = "~/_SiteLayout.cshtml";

    Business.Platform.WorkFlowDelegation bworkFlowDelegation = new Business.Platform.WorkFlowDelegation();
    Data.Model.WorkFlowDelegation workFlowDelegation = null;
    string id=Request.QueryString["id"];

    string UserID = string.Empty;
    string ToUserID = string.Empty;
    string StartTime = string.Empty;
    string EndTime = string.Empty;
    string FlowID = string.Empty;
    string Note = string.Empty;

    bool isOneSelf = "1" == Request.QueryString["isoneself"];
    
    if(isOneSelf)
    {
        UserID = Business.Platform.Users.PREFIX + Business.Platform.Users.CurrentUserID.ToString();
    }
    
    Guid delegationID;
    if (id.IsGuid(out delegationID))
    {
        workFlowDelegation = bworkFlowDelegation.Get(delegationID);
        if(workFlowDelegation!=null)
        {
            UserID = Business.Platform.Users.PREFIX + workFlowDelegation.UserID.ToString();
            ToUserID = Business.Platform.Users.PREFIX + workFlowDelegation.ToUserID.ToString();
            StartTime = workFlowDelegation.StartTime.ToString("yyyy/MM/dd HH:mm");
            EndTime = workFlowDelegation.EndTime.ToString("yyyy/MM/dd HH:mm");
            FlowID = workFlowDelegation.FlowID.ToString();
            Note = workFlowDelegation.Note;
        }
    }
    string oldXML = workFlowDelegation.Serialize();
   
    if(IsPost && Validation.IsValid())
    {
        UserID = Request.Form["UserID"];
        ToUserID = Request.Form["ToUserID"];
        StartTime = Request.Form["StartTime"];
        EndTime = Request.Form["EndTime"];
        FlowID = Request.Form["FlowID"];
        Note = Request.Form["Note"];

        bool isAdd = false;
        if (workFlowDelegation == null)
        {
            isAdd = true;
            workFlowDelegation = new Data.Model.WorkFlowDelegation();
            workFlowDelegation.ID = Guid.NewGuid();
        }
        workFlowDelegation.UserID = Business.Platform.Users.RemovePrefix(UserID).ToGuid();
        workFlowDelegation.EndTime = EndTime.ToDateTime();
        if(FlowID.IsGuid())
        {
            workFlowDelegation.FlowID = FlowID.ToGuid();
        }
        workFlowDelegation.Note = Note.IsNullOrEmpty() ? null : Note;
        workFlowDelegation.StartTime = StartTime.ToDateTime();
        workFlowDelegation.ToUserID = Business.Platform.Users.RemovePrefix(ToUserID).ToGuid();
        workFlowDelegation.WriteTime = Utility.DateTimeNew.Now;

        if(isAdd)
        {
            bworkFlowDelegation.Add(workFlowDelegation);
            Business.Platform.Log.Add("添加了工作委托", workFlowDelegation.Serialize(), Business.Platform.Log.Types.流程相关);
        }
        else
        {
            bworkFlowDelegation.Update(workFlowDelegation);
            Business.Platform.Log.Add("修改了工作委托", "修改前：" + oldXML + "修改后：" + workFlowDelegation.Serialize(), Business.Platform.Log.Types.流程相关);
        }
        bworkFlowDelegation.RefreshCache();
        <script type="text/javascript">
            new RoadUI.Window().reloadOpener();
            alert("保存成功!");
        </script>
        
    }
    string flowOptions = new Business.Platform.WorkFlow().GetOptions(FlowID);
}

    <form method="post" action="">
    <br />
    <table cellpadding="0" cellspacing="1" border="0" width="99%" class="formtable">
    <tr>
        <th style="width:100px;">委托人：</th>
        <td><input type="text" name="UserID" @(isOneSelf?"disabled='disabled'":"") dept="0" station="0" unit="0" user="1" group="0" id="UserID" class="mymember" validate="empty" value="@UserID" /></td>
    </tr>
    <tr>
        <th>被委托人：</th>
        <td><input type="text" name="ToUserID" dept="0" station="0" unit="0" user="1" group="0" id="ToUserID" class="mymember" validate="empty" value="@ToUserID" /></td>
    </tr>
    <tr>
        <th>开始时间：</th>
        <td><input type="text" name="StartTime" id="StartTime" class="mycalendar" istime="1" validate="empty" value="@StartTime" /></td>
    </tr>
    <tr>
        <th>结束时间：</th>
        <td><input type="text" name="EndTime" id="EndTime" class="mycalendar" istime="1" validate="empty" value="@EndTime" /></td>
    </tr>
    <tr>
        <th>委托流程：</th>
        <td><select class="myselect" style="width:120px;" id="FlowID" name="FlowID"><option value="">==全部==</option>@Html.Raw(flowOptions)</select></td>
    </tr>
    <tr>
        <th>备注说明：</th>
        <td><textarea id="Note" name="Note" cols="1" rows="1" style="width:95%;height:50px;">@Note</textarea></td>
    </tr>
    
    </table>
    <div class="buttondiv">
        <input type="submit" value="确定保存" class="mybutton" onclick="return new RoadUI.Validate().validateForm(document.forms[0]);" />
        <input type="button" class="mybutton" value="取消关闭" style="margin-left: 5px;" onclick="new RoadUI.Window().close();" />
    </div>
    </form>